<?php

namespace App\Http\Service\Baoliao;

use App\Models\Baoliaolog;
use App\Models\Baoliaopost;
use App\Models\Baoliaopostbak;
use App\Models\Logerror;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use App\Http\Service\BaseService;

class BaoliaopostService  extends BaseService
{
    private $url;
    private $getInfo;
    private $model;

    public function __construct(Baoliaopost $model,Baoliaolog $baoliaolog,Logerror $logerror,Baoliaopostbak $baoliaopostbak)
    {
        parent::__construct();
        $this->model = $model;
        $this->baoliaolog=$baoliaolog;
        $this->logerror=$logerror;
        $this->baoliaopostbak=$baoliaopostbak;
        $this->url=config('myurl.link_parse');
        $this->getInfo=config('myurl.getInfo');


    }

    //存入日志错误信息
    public function addDataLog($request,$data=[])
    {
        $unique_id=$data['unique_id'];
        $first=$this->logerror->where(['unique_id'=>$unique_id])->first();
        if($first){
            $data['create_time'] =date("Y-m-d H:i:s");
            $res=$this->logerror->where(['unique_id'=>$unique_id])->update($data);
        }else{
            $data['create_time'] =date("Y-m-d H:i:s");
            $data['update_time'] =date("Y-m-d H:i:s");
            $res=$this->logerror->insertGetId($data);
        }
        if($res){
            $this->success1();
        }else{
            $this->error1();
        }

    }

    //爆料推送消息-备份
    public function baoliaoPushbak($request,$data=[])
    {
        $unique_id=$data['unique_id'];

        //获取所有json数据
        $jsonData = $request->json()->all();
        $data['json_data']=json_encode($jsonData, JSON_UNESCAPED_UNICODE);
        //dd($data['json_data']);
        $first=$this->baoliaopostbak->where(['unique_id'=>$unique_id])->first();

        if($first){
            $data['update_time'] =date("Y-m-d H:i:s");
            $res=$this->baoliaopostbak->where(['unique_id'=>$unique_id])->update($data);
            $post_id=$first['id'];

        }else{
            $data['create_time'] =date("Y-m-d H:i:s");
            $data['update_time'] =date("Y-m-d H:i:s");
            
            $data['cover'] ='';
            $res=$post_id=$this->baoliaopostbak->insertGetId($data);
        }
        $data['id']=$post_id;
        if($res){
            $this->success($data['json_data']);
        }else{
            $this->error();
        }
    }
   

    public function addData($request,$data=[])
    {
        $unique_id=$data['unique_id'];

        //获取所有json数据
        $jsonData = $request->json()->all();
        $data['json_data']=json_encode($jsonData, JSON_UNESCAPED_UNICODE);
        //dd($data);
        $first=$this->model->where(['unique_id'=>$unique_id])->first();

        if($first){
            $data['update_time'] =date("Y-m-d H:i:s");
            $res=$this->model->where(['unique_id'=>$unique_id])->update($data);
            $post_id=$first['id'];

        }else{
            $data['create_time'] =date("Y-m-d H:i:s");
            $data['update_time'] =date("Y-m-d H:i:s");
            $res=$post_id=$this->model->insertGetId($data);
        }
        $data['id']=$post_id;
        if($res){
            $this->success1();
        }else{
            $this->error1();
        }
    }
   
    
}
